package sketch.specs;

import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

import sketch.ast.ASTUtils;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class UnusedVarDeclarationTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(UnusedVarDeclarationTest.class);
	}
	
	public void testNotRemovedVariable() {
		String filePath = "./testfiles/sketch/specs/SampleCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getMethodsWithAnnotation(unit, "TestSketch");
		for(MethodDeclaration method : methods) {
			Block block = method.getBody();
			UnusedVarDeclarationRemover remover = new UnusedVarDeclarationRemover(block);
			List<VariableDeclarationStatement> removables = remover.getAllRemovableVarDeclarationStatements();
			System.out.println("For method: " + method.getName() + ", number of removable statements: " + removables.size());
			assertTrue(removables.isEmpty());
		}
		System.out.println("\n\n");
	}
	
	public void testRemovedVariable() {
		String filePath = "./testfiles/sketch/specs/RedundantCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getMethodsWithAnnotation(unit, "TestSketch");
		//get the comment list
		for(MethodDeclaration method : methods) {
			Block block = method.getBody();
			UnusedVarDeclarationRemover remover = new UnusedVarDeclarationRemover(block);
			List<VariableDeclarationStatement> removables = remover.getAllRemovableVarDeclarationStatements();
			System.out.println("For method: " + method.getName() + ", number of removable statements: " + removables.size());
			for(VariableDeclarationStatement statement : removables) {
				System.out.println("   " + statement);
			}
			assertTrue(removables.size() == 1);
		}
	}
	
	public void testRemovableVariablesScopes() {
		String filePath = "./testfiles/sketch/ast/ValDecl.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<MethodDeclaration> methods = ASTUtils.getMethodsWithAnnotation(unit, "TestSketch");
		for(MethodDeclaration method : methods) {
			Block block = method.getBody();
			//use recursively
			UnusedVarDeclarationRemover remover = new UnusedVarDeclarationRemover(block, true /*means recursively find all*/);
			List<VariableDeclarationStatement> removables = remover.getAllRemovableVarDeclarationStatements();
			System.out.println("For method: " + method.getName() + ", number of removable statements: " + removables.size());
			for(VariableDeclarationStatement statement : removables) {
				System.out.println("   " + statement);
			}
			if(method.getName().getIdentifier().equals("rewriteTestConstructorException")) {
				assertEquals(2, removables.size());
			}
			if(method.getName().getIdentifier().equals("rewriteTestConstructorException0")) {
				assertEquals(2, removables.size());
			}
			if(method.getName().getIdentifier().equals("testVariable")) {
				assertEquals(1, removables.size());
			}
		}
	}
}
